﻿#include<iostream>
#include<assert.h>
using namespace std;


template <class K, class V>
struct AVLTreeNode
{
	pair<K, V> _kv;
	AVLTreeNode* _left;
	AVLTreeNode* _right;
	AVLTreeNode* _parent;
	int _bf;// 平衡因子

	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}
};
template <class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		// 链接父亲
		cur->_parent = parent;
		// 更新平衡因子
		while (parent)
		{
			if (cur == parent->_right)// 插入右边平衡因子++
				++parent->_bf;
			else
				--parent->_bf; // 左边--
			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1) // 继续向上更新
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				if (parent->_bf == -2 && cur->_bf == -1)// 右单旋
					RotateR(parent);
				else if (parent->_bf == 2 && cur->_bf == 1)
					RotateL(parent);
				else if (parent->_bf == -2 && cur->_bf == 1)
					RotateLR(parent);
				else if (parent->_bf == 2 && cur->_bf == -1)
					RotateRL(parent);
				else
					assert(false);
				break;
			}
			else
				assert(false);
		}
		return true;
	}
	// 右单旋
	void RotateR(Node * parent)
	{
		// subL当根节点，parent当subL的右子树，subLR当parent的右子树，注意所有的parent的处理
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		subL->_right = parent;
		parent->_left = subLR;
		// 从下往上链接父节点
		if (subLR)// subL可能为空，为空就不需要处理父亲节点
			subLR->_parent = parent;
		Node* pParent = parent->_parent;// 父节点的父亲，便于后续链接
		parent->_parent = subL;
		if (parent == _root)// 如果调整的是整棵树的根
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else// 局部树，链接祖先
		{
			if (parent == pParent->_left)
				pParent->_left = subL;
			else
				pParent->_right = subL;
			subL->_parent = pParent;
		}
		// 更新平衡因子
		subL->_bf = parent->_bf = 0;
	}

	//左单旋
	void RotateL(Node * parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		subR->_left = parent;
		// 从下往上链接父节点
		if (subRL)
			subRL->_parent = parent;
		Node* pParent = parent->_parent;
		parent->_parent = subR;
		if (parent == _root)// 调整的是整棵树的根
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == pParent->_left)
				pParent->_left = subR;
			else
				pParent->_right = subR;
			subR->_parent = pParent;
		}
		subR->_bf = parent->_bf = 0;
	}

	void RotateLR(Node * parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;// 记录其平衡因子，看新节点是插入在左还是在右
		RotateL(subL);// 先左旋
		RotateR(parent);// 再右旋
		// 更新平衡因子
		if (bf == 1)// 在右侧插入的节点
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else if (bf == -1)// 左侧
		{
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 0)// 树为空直接插入
		{
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else
			assert(false);
	}
	void RotateRL(Node * parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf; // 记录其平衡因子，看新节点是插入在左还是在右
		// 先右旋
		RotateR(subR);
		// 左旋
		RotateL(parent);
		if (bf == -1)// 左边插入 
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = -1;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else
			assert(false);
	}

	Node* Find(const K & key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key > cur->_kv.first)// 大的往右走
				cur = cur->_right;
			else if (key < cur->_kv.first)// 小的往左走
				cur = cur->_left;
			else
				return cur;
		}
		return nullptr;
	}
	void InOrder()
	{
		_InOrder(_root);
	}
	int Height()
	{
		return _Height(_root);
	}
	int Size()
	{
		return _Size(_root);
	}
	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}
private:
	int _Height(Node * root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;
		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	void _InOrder(Node * root)
	{
		if (root == nullptr)
			return;
		_InOrder(root->_left);
		cout << root->_kv.first << " " << root->_kv.second << endl;
		_InOrder(root->_right);
	}
	bool _IsBalanceTree(Node * root)
	{
		if (root == nullptr)
			return true;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常，不是AVL树！！！" << endl;
			return false;
		}
		if (diff != root->_bf)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}
	Node* _root = nullptr;
};
